package cn.edu.jxau.test;

import cn.edu.jxau.util.LinkedStack;
import cn.edu.jxau.util.Stack;

/**
 * @author Fudashi
 */
public class Main {

	public static void main(String[] args) {

		Queue<Integer> queue = new Queue<>();
		queue.appendTail(1);
		queue.appendTail(2);
		queue.appendTail(3);
		System.out.println(queue.deleteHead());
		queue.appendTail(4);
		queue.appendTail(5);
		queue.appendTail(6);
		System.out.println(queue.deleteHead());
		queue.appendTail(7);
		System.out.println(queue.deleteHead());
		queue.appendTail(8);
		System.out.println(queue.deleteHead());
		System.out.println(queue.deleteHead());
		queue.appendTail(9);
		System.out.println(queue.deleteHead());
		System.out.println(queue.deleteHead());
		System.out.println(queue.deleteHead());

	}
}

/**
 * 用两个Stack实现一个队列
 * 
 * @author Fudashi
 */
class Queue<T> {

	private Stack<T> stack1;
	private Stack<T> stack2;

	public Queue() {

		stack1 = new LinkedStack<>();
		stack2 = new LinkedStack<>();
	}

	public void appendTail(T data) {
		stack1.push(data);
	}

	public T deleteHead() {

		if (stack2.isEmpty()) {
			while (!stack1.isEmpty()) {
				stack2.push(stack1.pop());
			}
		}
		if(stack2.isEmpty()) {
			throw new RuntimeException("the queue is empty");
		}
		return stack2.pop();
	}
}